package system

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/system"
	systemReq "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type Certificate_managementApi struct{}

// CreateCertificate_management 创建证书管理
// @Tags Certificate_management
// @Summary 创建证书管理
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body system.Certificate_management true "创建证书管理"
// @Success 200 {object} response.Response{msg=string} "创建成功"
// @Router /certificate_management/createCertificate_management [post]
func (certificate_managementApi *Certificate_managementApi) CreateCertificate_management(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	var certificate_management system.Certificate_management
	err := c.ShouldBindJSON(&certificate_management)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = certificate_managementService.CreateCertificate_management(ctx, &certificate_management)
	if err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("创建成功", c)
}

// DeleteCertificate_management 删除证书管理
// @Tags Certificate_management
// @Summary 删除证书管理
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body system.Certificate_management true "删除证书管理"
// @Success 200 {object} response.Response{msg=string} "删除成功"
// @Router /certificate_management/deleteCertificate_management [delete]
func (certificate_managementApi *Certificate_managementApi) DeleteCertificate_management(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	ID := c.Query("ID")
	err := certificate_managementService.DeleteCertificate_management(ctx, ID)
	if err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("删除成功", c)
}

// DeleteCertificate_managementByIds 批量删除证书管理
// @Tags Certificate_management
// @Summary 批量删除证书管理
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{msg=string} "批量删除成功"
// @Router /certificate_management/deleteCertificate_managementByIds [delete]
func (certificate_managementApi *Certificate_managementApi) DeleteCertificate_managementByIds(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	IDs := c.QueryArray("IDs[]")
	err := certificate_managementService.DeleteCertificate_managementByIds(ctx, IDs)
	if err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("批量删除成功", c)
}

// UpdateCertificate_management 更新证书管理
// @Tags Certificate_management
// @Summary 更新证书管理
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body system.Certificate_management true "更新证书管理"
// @Success 200 {object} response.Response{msg=string} "更新成功"
// @Router /certificate_management/updateCertificate_management [put]
func (certificate_managementApi *Certificate_managementApi) UpdateCertificate_management(c *gin.Context) {
	// 从ctx获取标准context进行业务行为
	ctx := c.Request.Context()

	var certificate_management system.Certificate_management
	err := c.ShouldBindJSON(&certificate_management)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = certificate_managementService.UpdateCertificate_management(ctx, certificate_management)
	if err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("更新成功", c)
}

// FindCertificate_management 用id查询证书管理
// @Tags Certificate_management
// @Summary 用id查询证书管理
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param ID query uint true "用id查询证书管理"
// @Success 200 {object} response.Response{data=system.Certificate_management,msg=string} "查询成功"
// @Router /certificate_management/findCertificate_management [get]
func (certificate_managementApi *Certificate_managementApi) FindCertificate_management(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	ID := c.Query("ID")
	recertificate_management, err := certificate_managementService.GetCertificate_management(ctx, ID)
	if err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败:"+err.Error(), c)
		return
	}
	response.OkWithData(recertificate_management, c)
}

// GetCertificate_managementList 分页获取证书管理列表
// @Tags Certificate_management
// @Summary 分页获取证书管理列表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data query systemReq.Certificate_managementSearch true "分页获取证书管理列表"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
// @Router /certificate_management/getCertificate_managementList [get]
func (certificate_managementApi *Certificate_managementApi) GetCertificate_managementList(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	var pageInfo systemReq.Certificate_managementSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := certificate_managementService.GetCertificate_managementInfoList(ctx, pageInfo)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败:"+err.Error(), c)
		return
	}
	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "获取成功", c)
}

// GetCertificate_managementPublic 不需要鉴权的证书管理接口
// @Tags Certificate_management
// @Summary 不需要鉴权的证书管理接口
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{data=object,msg=string} "获取成功"
// @Router /certificate_management/getCertificate_managementPublic [get]
func (certificate_managementApi *Certificate_managementApi) GetCertificate_managementPublic(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	// 此接口不需要鉴权
	// 示例为返回了一个固定的消息接口，一般本接口用于C端服务，需要自己实现业务逻辑
	certificate_managementService.GetCertificate_managementPublic(ctx)
	response.OkWithDetailed(gin.H{
		"info": "不需要鉴权的证书管理接口信息",
	}, "获取成功", c)
}
